<%@page import="com.egame.manage.biz.domain.TerminalGroupInfoBean"%>
<%@page import="cn.egame.interfaces.EnumType.AppParameterType"%>
<%@page import="cn.egame.interfaces.gc.TerminalInfo"%>
<%@page import="cn.egame.common.util.Utils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="cn.egame.interfaces.pu.AppParameter"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
	List<String[]> brandMap = new ArrayList<String[]>();
	if(request.getAttribute("brandList") != null) {
		brandMap = (List<String[]>)request.getAttribute("brandList");	
	}
	
	TerminalGroupInfoBean terminalGroupInfoBean = new TerminalGroupInfoBean();
	if(request.getAttribute("terminalGroupInfoBean") != null){
		terminalGroupInfoBean = (TerminalGroupInfoBean)
				request.getAttribute("terminalGroupInfoBean");
	}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet"
	href="<%=request.getContextPath()%>/css/main.css" />
<script type="text/javascript"
	src="<%=request.getContextPath()%>/js/common.js"></script>
<title>新增机型组</title>
<script type="text/javascript"
	src="<%=request.getContextPath()%>/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
	function isTerminalGroupExist(){
		var exist = true;
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/terminalgroup/checkExist.do" %>",
			type : 'post',
			data : {
				'name' : $("#name").val(),
				'id' : $("#id").val()
			},
			success:function (data) {
				if (data.retCode == "success" && data.exist=="notExist") {
					exist = false;
				}
			}, 
			error:function () {
				alert("error");
			},
			async : false
		});
		return exist;
	}

	function checkName(){
		var td = $("#form1").find("td[name='notnone']");
		if($(td).find("input").val() == "" || $(td).find("input").val().length>20){
			$(td).find("span").remove();
			$(td).append("<span class='page_tip'>*必填，最多20个汉字字符</span>");
			$(td).focus();
			return false;
		}else{
			$(td).find("span").remove();
		}
		return true;
	}
	
	function checkRemark(){
		var td = $("#form1").find("td[name='max20']");
		if($(td).find("input").val().length>20){
			$(td).find("span").remove();
			$(td).append("<span class='page_tip'>最多20个汉字字符</span>");
			$(td).focus();
			return false;
		}else{
			$(td).find("span").remove();
		}
		return true;
	}
	function sub(){
		var ck = checkName() & checkRemark();
		if(ck){
			if(!isTerminalGroupExist()){
				$("#form1").submit();
			}else{
				var td = $("#form1").find("td[name='notnone']");
				$(td).find("span").remove();
				$(td).append("<span class='page_tip'>组名已经存在</span>");
				$(td).focus;
			}
		}
	}
	
	function back(){
		window.history.back();
	}
	function getTerminal(){
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/terminalgroup/terminal/getUnrelated.do" %>",
			type : 'post',
			data : {
				'brand' : $("#brand").val(),
				'fromName' : $("#fromName").val()
			},
			success:function (data) {
				if (data && data.retCode == "success") {
					$("#fromTerminal").empty();
					$.each(data.terminalInfoListFilter, function (i, item) {
						$("#fromTerminal").append("<option value='"+item.terminalId+"'>"+item.uacode+"</option>");
					});
				}
			}, 
			error:function () {
				alert("error");
			},
			async : false
		});	
	}
	
	function getTerminalRelated(){
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/terminalgroup/terminal/getRelated.do" %>",
			type : 'post',
			data : {
				'toName' : $("#toName").val()
			},
			success:function (data) {
				if (data && data.retCode == "success") {
					$("#toTerminal").empty();
					$.each(data.terminalInfoListRelatedFilter, function (i, item) {
						$("#toTerminal").append("<option value='"+item.terminalId+"'>"+item.uacode+"</option>");
					});
				}
			}, 
			error:function () {
				alert("error");
			},
			async : false
		});	
	}
	
	function addRelatedTerminal() {	
		checkValue("fromTerminal");				
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/terminalgroup/terminal/add.do" %>",
			type : 'post',
			data : {
				'terminalIdString' : getOptionValueList("fromTerminal").toString()
			},
			success:function (data) {
				if (data && data.retCode == "success") {
					$("#fromTerminal").empty();
					$("#toTerminal").empty();
					$.each(data.terminalInfoListFilter, function (i, item) {
						$("#fromTerminal").append("<option value='"+item.terminalId+"'>"+item.uacode+"</option>");
					});
					$.each(data.terminalInfoListRelatedFilter, function (i, item) {
						$("#toTerminal").append("<option value='"+item.terminalId+"'>"+item.uacode+"</option>");
					});
				}
			}, 
			error:function () {
				alert("error");
			},
			async : false
		});	
		getTerminal();
		getTerminalRelated();
	}
	
	function deleteRelatedTerminal() {	
		checkValue("toTerminal");				
		$.ajax({			
			url : "<%=request.getContextPath() + "/tv/terminalgroup/terminal/del.do" %>",
			type : 'post',
			data : {
				'terminalIdString' : getOptionValueList("toTerminal").toString()
			},
			success:function (data) {
				if (data && data.retCode == "success") {
					$("#fromTerminal").empty();
					$("#toTerminal").empty();
					$.each(data.terminalInfoList, function (i, item) {
						$("#fromTerminal").append("<option value='"+item.terminalId+"'>"+item.uacode+"</option>");
					});
					$.each(data.terminalInfoListRelated, function (i, item) {
						$("#toTerminal").append("<option value='"+item.terminalId+"'>"+item.uacode+"</option>");
					});
				}
			}, 
			error:function () {
				alert("error");
			},
			async : false
		});		
		getTerminal();
		getTerminalRelated();
	}
	
	function checkValue(terminalIds){
		terminalIdsValue = getOptionValueList(terminalIds);
		
		if(terminalIdsValue == null || terminalIdsValue == ""){
			alert("请选择机型");
			return;
		}
	}
	function getOptionValueList(selectId){
		var options = document.getElementById(selectId);
		var result = new Array();
		for(var i=0;i<options.length;i++){
			if(options[i].selected == true){
				result.push(options[i].value);
			}
		}
		return result;
	}
	
</script>
</head>
<body>
	<div class="matter_box">
		<div class="mt10 pt10 clearfix">
			<form
				action="<%=request.getContextPath() + "/tv/terminalgroup/update.do"%>"
				method="post" id="form1">
				<input type="hidden" name="id" id="id" value="<%=terminalGroupInfoBean.getId() %>"> 
				
				<table class="table2">
					<tr>
						<td align=right>机型组名称:</td>
						<td name="notnone">
							<input type=text name="name" id="name" value="<%=terminalGroupInfoBean.getName() %>" maxlength="20">
						</td>
					</tr>
					<tr>
						<td align=right>备注:</td>
						<td name="max20">
							<input type=text name="remark" id="remark" value="<%=terminalGroupInfoBean.getRemark() %>" maxlength="20">
						</td>
					</tr>
					<tr>
						<td colspan=2>
							<table>
								<tr>
									<td>待关联终端</td>
									<td>
										<input type=button value="添加 >" class="btn3"  onclick="addRelatedTerminal()"> <br/>
										<input type="button" class="btn3" onclick="deleteRelatedTerminal()" value="< 删除"><br/>
									</td>
									<td>已关联终端</td>
								</tr>
								<tr>
									<td>
										品牌：
										<select name="brand" id="brand">
											<option value="0">请选择</option>
											<%  if(brandMap != null){          					
						          					for(String[] brand:brandMap) { 
						          						if(brand.length==3){
						          			%>          					
						          					<option value="<%=brand[0] %>"><%=brand[1] %>-<%=brand[2] %></option>
						          			<%	
						          						}}
							          			}
						          			%>
										</select>
										<p>
										客户端UA：
										<input type=text name="fromName" id="fromName" >
										<p>
										<input type="button" value="查询" onclick="getTerminal()">
									</td>
									<td></td>
									<td>
										客户端UA：
										<input type=text name="toName" id="toName">
										<p>
										<input type="button" value="查询" onclick="getTerminalRelated()">
									</td>
								</tr>
								<tr>
									<td>
										<select id = "fromTerminal" name = "fromTerminal" multiple="multiple" size=20 style="width: 240px">
											<%
												if(session.getAttribute("terminalInfoList")!=null){
													Set<TerminalInfo> set = (Set<TerminalInfo>)session.getAttribute("terminalInfoList");
													for(TerminalInfo bean:set){
														%>
														<option value="<%=bean.getTerminalId() %>" 
															
														><%=bean.getUACode() %></option>
														<%
													}
												}
											%>
										</select>
									</td>
									<td>
									</td>
									<td>
										<select id = "toTerminal" name = "toTerminal" multiple="multiple" size=20 style="width: 240px">
											<%
												if(session.getAttribute("terminalInfoListRelated")!=null){
													Set<TerminalInfo> set = (Set<TerminalInfo>)session.getAttribute("terminalInfoListRelated");
													for(TerminalInfo bean:set){
														%>
														<option value="<%=bean.getTerminalId() %>" 
															
														><%=bean.getUACode() %></option>
														<%
													}
												}
											%>
										</select>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan=2>
							<input type="button" value="确定" onclick="sub()">
							<input type="button" value="返回" onclick="back()">
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</body>
</html>